package com.byzx.wine.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzx.wine.po.Purchase;
import com.byzx.wine.service.IPurchaseService;
import com.byzx.wine.util.Page;

@RequestMapping("/purchase")
@Controller
public class PurchaseController {
	@Autowired
	IPurchaseService   purchaseservice;
	
	 @RequestMapping("purchaseentert")
		public String login(HttpServletResponse response, HttpServletRequest request) {
			return "purchase/purchase";
		}
	@RequestMapping("findpage")
	@ResponseBody
	public Map<String, Object> findPage(Model model, Integer iDisplayLength, Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//构造方法传参
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(iDisplayStart, iDisplayLength, map);
	
		Page<Map<String, Object>> page1 = purchaseservice.findPage(page);
		
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
	@RequestMapping("/added")
	@ResponseBody
	public  int insert(Purchase purchase){
		int num=purchaseservice.insert(purchase);
		return num;
	}
	@RequestMapping("/select")
	@ResponseBody
	public Map<String,Object> selectupdate(Integer id){
		Purchase purch=	purchaseservice.selectByPrimaryKey(id);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("purch", purch);
		return map;
	}
	@RequestMapping("/update")
	@ResponseBody
	public Map<String,Object> update(Purchase purchase){
		int num=purchaseservice.updateByPrimaryKeySelective(purchase);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}
	@RequestMapping("delete")
	@ResponseBody
	public int delete(Integer id){
	int num=purchaseservice.deleteByPrimaryKey(id);
	return num;
	}
	@RequestMapping("deleteall")
	@ResponseBody
	public int deleteall(String id){
		//先定义数组接收并去掉，号
		String shuzu[]=id.split(",");
		int num=0;
		for(int i=0;i<shuzu.length;i++){
			//转化成int类型
			int ids=Integer.parseInt(shuzu[i]);
			//调用删除方法
			 num=purchaseservice.deleteByPrimaryKey(ids);
			
		}
		return num;
	}
	
	@RequestMapping("findall")
	@ResponseBody
	public Map<String,Object> findall(Integer id){
		Purchase purch=	purchaseservice.selectByPrimaryKey(id);
		
		List<Map<String,Object>> list=purchaseservice.findall(id);
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("purch", purch);
		map.put("list", list);
		return map;
	}
}
